home *** CD-ROM | disk | FTP | other *** search
- ;
- ; UDP receive code
- ;
- ; This receives data on its socket (which is bound to a port
- ; on localhost), from any host that sends to it.
- ;
- ; Written by Anton Reinauer <anton@ww.co.nz>.
- ;
- ; Thanks to Paul Burkey for TCP_Funcs and to Dr. Ercole Spiteri
- ; for TCP-to-Blitz.
- ;
-
-
- WBStartup
- NoCli
- Hostname.s="localhost" ; you need to bind socket to localhost
- #PORT=3001 ; a free port to bind to- it can't
- ; be a port used in Services.
-
- INCLUDE "TCPFuncs.bb"
- DEFTYPE .w
-
- ;********************************************************
-
- .ReadUDP
- Function .s ReadUDP{}
- SHARED sock.l,TCPmem.l
-
- ; This Function reads data from the socket it is bound to.
- ; If there is no messages then it will return an empty string =""
-
- sockread.l=0 ;Clear Readmask
- sockread.l BitSet sock.l ;Set Readmask on our socket
- e=IoctlSocket_(sock.l,#FIONREAD,TCPmem.l) ;How much data is there?
- f.l=Peek.l(TCPmem.l) ;Place value in f
-
- If f>0 ; any data waiting
-
- c=recvfrom_(sock.l,TCPmem.l,f,0,0,messagelen) ;Read all data waiting on socket
-
- If c>0 ; if there any data waiting
- a=0 : c$=""
- Repeat
- c$=c$+Chr$(Peek.b(TCPmem+a)) : a+1 ; build string
- Until a=c
- EndIf
-
- EndIf
-
- Function Return c$
- End Function
-
- .ConnectUDP2
- Function ConnectUDP2{host$,port.w}
- SHARED sock.l,host.sockaddrin,hostlen.w
- ;
- ; Connect to host at specified port
- ; Return true or False if Connection is made
- ;
-
- sock.l=socket_(2,2,0) ; open UDP socket
-
- *a.hostent=gethostbyname_(host$) ; get local host structure
-
- ;Copy Details to our Sockaddrin structure
-
- bb=CopyMem_(*a.hostent\h_addr_list\ItemA,&host.sockaddrin\sin_addr,*a.hostent\h_length)
-
- host.sockaddrin\sin_port=port ;Set local port number
- host.sockaddrin\sin_family=2 ;Set type to AT_INET
- hostlen=SizeOf.sockaddrin ;Get lenght of structure sockaddrin
-
- If bind_(sock,host,hostlen)=0 ; bind socket to port so we can
- Function Return True ; receive data on port
- EndIf
-
- End Function
-
- ;****************************************
-
-
- WbToScreen0
- Window 0,300,100,320,150,$1|$2|$4|$8|$400,"Receive UDP",1,2
-
- WindowOutput0
- WindowInput 0
-
- If ConnectUDP2 {Hostname,#PORT} ; bind socket to local port
- NPrint "Bound to ",Hostname," ",#PORT
- Else
- NPrint "Can't bind to ",Hostname," ",#PORT
- Goto Exit
- EndIf
-
- ypos=10
-
- ;****************************************
-
- .Main
-
- Repeat
- Delay_(1)
-
- a$=ReadUDP{} ; get data from socket
-
- If a$<>""
- t$=a$
- Gosub Print_String
- EndIf
-
- ev.l=Event
- Until ev=$200 ; shut down if close gadget hit
-
-
- CloseTCP{} ; close connection
-
- WLocate 10,10
- Print "Connection closed"
-
- Exit:
- Delay_(50)
- FreeMem TCPmem,$2000
-
- End
-
- ;*********************** Gosubs *********************
-
- .Print_String
- ypos+10
- If ypos=130 Then ypos=20
- WLocate 10,ypos
- Print " "
- WLocate 10,ypos
- Print "RECEIVED: ",t$ ; print string received
- Return
-
-